package Gui;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class DiceImagePanel extends JPanel {
	private BufferedImage m_img;
	private DiceImagePanel m_ref;
	private static final int m_sWidth = 55, m_sHeight = 55, m_sRows = 2,
			m_sColumns = 6;
	private  ArrayList<ImagePanel> m_diceSprites;

	// assumes getInstance() has been called;
	// sets up sprite list
	private  void initSprites() {
		m_diceSprites = new ArrayList<ImagePanel>();
		for (int y = 0; y < m_sRows; ++y) {
			for (int x = 0; x < m_sColumns; ++x) {
				m_diceSprites.add(new ImagePanel(m_img.getSubimage(x
						* m_sWidth, y * m_sHeight, m_sWidth, m_sHeight)));
			}
		}

	}
	
	
	public ImagePanel getDicePanel(boolean isWhiteDie, int faceValue){
		System.out.println(faceValue);
		ImagePanel jp = m_diceSprites.get((isWhiteDie?0:6) + faceValue - 1);
		jp.setBorder(BorderFactory.createRaisedBevelBorder());
		return jp;
	}
	private DiceImagePanel(BufferedImage img) {
		setLayout(new BorderLayout());
		this.m_img = img;
		initSprites();
	}

	private DiceImagePanel(String path) throws IOException {
		this(ImageIO.read(new File(path)));
		setLayout(new BorderLayout());
		
	}
	public DiceImagePanel() throws IOException{
		this("Images\\Dice-Sprite-Sheet-55x55.jpg"); 
	}
	

	public void paintComponent(Graphics g) {
		int x = (getWidth()-m_img.getWidth())/2;
		int y = (getHeight()-m_img.getHeight())/2;
		g.drawImage(m_img, x, y, null);
	}
	
}
